#!/usr/bin/env python
#coding:utf-8

import os,sys
import pexpect
from mod_mysql import *
reload(sys)
sys.setdefaultencoding('utf8')

s = conn_mysql()
user = os.popen('whoami').read().split()[0]
ip = s.r_ip(user)
#ip.append('10.16.48.125')
pwd = s.s_pass(user)

while True:
    try:
        print '\033[33m欢迎使用跳板机，线上机器，谨慎操作，操作记录已入库！！！\033[0m'
        for i in ip:
            print '\033[32;1m %s \033[0m'%i,
        print '\n'
        option = raw_input('选择你要连接的机器:').strip()
        if option in ip:
            p =  pexpect.spawn("ssh %s@%s" %(user,option))
            try:
                 i = p.expect(['password:', 'continue connecting (yes/no)?'],timeout=5)
                 if i == 0:
                     p.sendline(pwd)
                     p.interact()
                 elif i == 1:
                     p.sendline("yes")
                     p.expect(['password:'])
                     p.sendline(pwd)
                     p.interact()
            except pexpect.TIMEOUT:
                print 'Connection timeout'
            except pexpect.EOF:
                print 'Connection exit'
                p.close()
        else:
            print 'Input error!'
    except (ValueError):
        print 'Wrong value!'
    except (EOFError):
        print '\n\033[31m拜拜!!!\033[0m'
	sys.exit()
    except (KeyboardInterrupt):
        print '\n\033[31m拜拜!!!\033[0m'
	sys.exit()
